﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_ManageSkills : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        (Master.FindControl("PageSubtitleLabel") as Label).Text = "Create and manage skills";

        if (!Page.IsPostBack)
        {
            FilterSkills();
        }
    }

    private void FilterSkills()
    {
        DSSkills.Where = "";

        if (lvCategories.SelectedIndex != -1)
        {
            string id = lvCategories.SelectedDataKey.Value.ToString();

            DSSkills.Where = "category_id == @category_id";
            DSSkills.WhereParameters["category_id"].DefaultValue = id;
        }
    }

    protected void lvCategories_SelectedIndexChanged(object sender, EventArgs e)
    {
        FilterSkills();
        lvSkills.DataBind();
    }

    protected void lvSkills_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        DropDownList ddl = e.Item.FindControl("category_idList") as DropDownList;
        e.Values.Add("category_id", ddl.SelectedValue);
    }

    protected void lvSkills_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        DropDownList ddl = lvSkills.EditItem.FindControl("category_idList") as DropDownList;
        e.NewValues["category_id"] = ddl.SelectedValue;
    }

    protected void lvSkills_DataBound(object sender, EventArgs e)
    {
        // An edit item means we are in edit mode
        if (lvSkills.EditItem != null && lvSkills.EditItem.ItemType == ListViewItemType.DataItem)
        {
            DropDownList ddl = lvSkills.EditItem.FindControl("category_idList") as DropDownList;
            HiddenField hf = lvSkills.EditItem.FindControl("hiddenCatId") as HiddenField;

            ddl.SelectedValue = hf.Value;
        }
    }
    protected void lvCategories_ItemInserted(object sender, ListViewInsertedEventArgs e)
    {
        // New catagory needs to be reflected in skill drop-down lists
        lvSkills.DataBind();
    }

}
